<template>
  <dl id="tab">
    <dd v-for="(item, index) in tabbar" @click="activeindex = item.path" :key="index" @click.stop="item.callback">
      <div>
        <img :src="activeindex != item.path ? item.img : item.imghover"
          :class="activeindex == item.path ? 'activeimg' : ''" alt="" />
      </div>
      <span :class="activeindex == item.path ? 'active' : ''">{{
      $t(item.lang)
    }}</span>
    </dd>
  </dl>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { getToken } from "@/hooks/usegettoken";
import { reactive, ref, watch, defineEmits } from "vue";
import { useStore } from "vuex";
const store = useStore();
const router = useRouter();
// 获取当前路由
const getImg = (url) => {
  return new URL(`../assets/img/${url}`, import.meta.url).href;
};
let emit = defineEmits(["openTerouro"]);
const tabbar = reactive([
  {
    img: getImg("tab1.png"),
    imghover: getImg("tab1l.png"),
    lang: "language.tabbar.tabbar1",
    path: "/home",
    callback: () => {
      router.push("/home");
      activeindex.value = "/home";
    },
  },
  {
    img: getImg("tab2.png"),
    imghover: getImg("tab2l.png"),
    lang: "language.tabbar.tabbar2",
    path: "/Star",
    callback: () => {
      router.push("/Star");
      activeindex.value = "/Star";
    },
  },
  {
    img: getImg("tab3.png"),
    imghover: getImg("tab3l.png"),
    lang: "language.tabbar.tabbar3",
    path: "/Share",
    callback: () => {
      router.push("/Share");
      activeindex.value = "/Share";
    },
  },
  {
    img: getImg("tab4.png"),
    imghover: getImg("tab4l.png"),
    lang: "language.tabbar.tabbar4",
    path: "/Treasure",
    callback: () => {
      if (getToken()) {
        activeindex.value = "/Treasure";
        emit("openTerouro", true);
      } else {
        store.commit("SET_LOGIN", true);
      }
    },
  },
  {
    img: getImg("tab5.png"),
    imghover: getImg("tab5l.png"),
    lang: "language.tabbar.tabbar5",
    path: "/My",
    callback: () => {
      router.push("/My");
      activeindex.value = "/My";
    },
  },
]);
let activeindex = ref("/home");
watch(
  () => router.currentRoute.value,
  (newval) => {
    activeindex.value = newval.fullPath;
    // console.log( activeindex.value,555);
  },
  {
    deep: true,
    immediate: true,
  }
);
</script>
<style lang="scss" scoped>
#tab {
  width: 100%;
  height: 60px;
  background-color: rgb(37, 38, 43);
  position: fixed;
  width: 100vw;
  bottom: 0;
  left: 0;
  @include flex(row, space-between, flex-start);

  &::after {
    width: 140px;
    height: 140px;
    border-radius: 70px;
    position: absolute;
    top: -15px;
    left: 50%;
    margin-left: -70px;
    z-index: -1;
    background-color: rgb(37, 38, 43);
    content: "";
    display: block;
  }

  margin-top: 13px;

  dd:nth-child(3) {
    padding-bottom: 10px;

    img {
      margin-right: 5px;
    }
  }

  dd {
    width: 70px;
    @include flex(column, center, center);
    height: 60px;
    cursor: pointer;

    div {
      width: 30px;
      height: 30px;
      @include flex(row, center, flex-end);

      img {
        width: 25px;
        transition: all 0.5s;
      }

      .activeimg {
        width: 28px;
      }
    }

    span {
      font-size: 13px;
      color: #dcdcdc;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .active {
      color: #d7a50c;
    }
  }
}
</style>
